package baseObject;

import java.util.concurrent.CopyOnWriteArrayList;

public class Wall implements IWall{
	private CopyOnWriteArrayList<IBrick> listBrick;
	
	public Wall() {
		this.init();
	}
	
	public void init() {
		this.listBrick = new CopyOnWriteArrayList<IBrick>();
	}
	
	@Override
	public void addBrick(IBrick brick){
		this.listBrick.add(brick);
	}

	public CopyOnWriteArrayList<IBrick> getListBrick() {
		return listBrick;
	}

	public CopyOnWriteArrayList<IBrick> getListBrickBreakeable() {
		CopyOnWriteArrayList<IBrick> listBrickBreakeable = new CopyOnWriteArrayList<IBrick>();
		for(IBrick br : this.listBrick) {
			if(br.isBreakable()) {
				listBrickBreakeable.add(br);
			}
		}
		
		return listBrickBreakeable;
	}
	
	public CopyOnWriteArrayList<IBrick> getListBrickHit(IBall ball) {
		CopyOnWriteArrayList<IBrick> listBrickHit = new CopyOnWriteArrayList<IBrick>();
		
		for(IBrick br : this.getListBrick()){
			if(br.isBreakable() && br.getHitState(ball)!=IRectObj.eHitType.NO){
				if(br.getResistance() > 0){
					hit(ball);
					listBrickHit.add(br);
				}
			}
		}
		
		return listBrickHit;		
	}


	@Override
	public void hit(IBall b) {
		for(IBrick br : this.listBrick) {
			if(br.isAffectedBy(b)){
				if(br.isBreakable())
					br.hit();
				if(br.getResistance() <= 0){
					this.getListBrick().remove(br);
				}
			}
		}
		
	}
}
